package com.sae.vote.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sae.vote.entity.Items;
import com.sae.vote.entity.Subject;
import com.sae.vote.util.DbUtilsTemplate;

/**
 * 投票主题业务类
 * 
 */
@Repository
public class SubjectService {

	@Autowired
	private DbUtilsTemplate dbUtilsTemplate;

	/**
	 * 分页查找投票主题
	 * 
	 * @param page
	 *            页码
	 * @param count
	 *            查找数量
	 * @return
	 * @throws SQLException
	 */
	public List<Subject> getSubjectsPaged(int page, int count)
			throws SQLException {
		List<Subject> subjects = dbUtilsTemplate.getReadRunner().query(
				DbUtilsTemplate.S1 + " limit ?,?",
				new BeanListHandler<Subject>(Subject.class), page, count);
		for (Subject subject : subjects) {
			subject.setItems(dbUtilsTemplate.getReadRunner().query(
					DbUtilsTemplate.S6,
					new BeanListHandler<Items>(Items.class),
					subject.getSubjectId()));
		}
		return subjects;
	}

	/**
	 * 获取用户投票主题
	 * 
	 * @param userID
	 *            用户
	 * @param page
	 *            页码
	 * @param count
	 *            数量
	 * @return
	 * @throws SQLException
	 */
	public List<Subject> getMySubjectPaged(int userID, int page, int count)
			throws SQLException {
		return dbUtilsTemplate.getReadRunner().query(
				DbUtilsTemplate.S3 + " LIMIT ?,?",
				new BeanListHandler<Subject>(Subject.class), userID, page,
				count);
	}

	/**
	 * 保存主题
	 * 
	 * @param subject
	 * @return
	 * @throws SQLException
	 */
	public String save(Subject subject) throws SQLException {
		String id = UUID.randomUUID().toString();
		dbUtilsTemplate.getWriteRunner().update(DbUtilsTemplate.S4, id,
				subject.getSubjectTitle(), subject.getCreator(),
				subject.getBeginTime(), subject.getEndTime(),
				subject.getIsAnonym(), subject.getSubjectType(),
				subject.getState(), subject.getDescript(), subject.getCover());
		return id;
	}

	/**
	 * 保存选项
	 * 
	 * @param item
	 * @return
	 * @throws SQLException
	 */
	public String saveItem(Items item) throws SQLException {
		String id = UUID.randomUUID().toString();
		dbUtilsTemplate.getWriteRunner().update(DbUtilsTemplate.S5, id,
				item.getSubjectID(), item.getItem(), item.getNo());
		return id;
	}

	/**
	 * 通过id查找主题
	 * 
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public Subject findById(String id) throws SQLException {
		Subject subject = dbUtilsTemplate.getReadRunner()
				.query(DbUtilsTemplate.S7,
						new BeanHandler<Subject>(Subject.class), id);
		if (subject != null)
			subject.setItems(dbUtilsTemplate.getReadRunner().query(
					DbUtilsTemplate.S6,
					new BeanListHandler<Items>(Items.class),
					subject.getSubjectId()));
		return subject;
	}

	/**
	 * 通过选项id查找选项
	 * 
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public Items findItem(String id) throws SQLException {
		Items item = dbUtilsTemplate.getReadRunner().query(DbUtilsTemplate.S11,
				new BeanHandler<Items>(Items.class), id);
		return item;
	}

	/**
	 * 投票
	 * 
	 * @param id
	 *            选项id
	 * @param userID
	 *            用户
	 * @throws SQLException
	 */
	public void vote(String id, Long userID) throws SQLException {
		Items item = findItem(id);
		boolean hasVote = dbUtilsTemplate.getReadRunner().query(
				DbUtilsTemplate.S8, new ResultSetHandler<Boolean>() {
					@Override
					public Boolean handle(ResultSet rs) throws SQLException {
						while (rs.next()) {
							return rs.getBigDecimal(1).intValue() > 0;
						}
						return false;
					}
				}, item.getSubjectID(), userID);
		if (hasVote) {
			return;
		}
		dbUtilsTemplate.getWriteRunner().update(DbUtilsTemplate.S9,
				item.getSubjectID(), userID, id);
		dbUtilsTemplate.getWriteRunner().update(DbUtilsTemplate.S10, id);
	}
}
